package com.library.admin.listener;

import books.borrowing.entity.SubscriberEntity;

import com.library.admin.AdmLibrary;
import com.library.admin.form.UpdateOwnAccountForm;
import com.library.admin.service.LibraryService;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

public class UpdateOwnAccountListener implements ClickListener {
	private static final long serialVersionUID = -4201960629127434685L;
	
	private SubscriberEntity administrator;
	private UpdateOwnAccountForm administratorAccountForm;
	
	public UpdateOwnAccountListener(SubscriberEntity subscriber, UpdateOwnAccountForm administratorAccountForm) {
		this.administrator = subscriber;
		this.administratorAccountForm = administratorAccountForm;
	}

	public void buttonClick(ClickEvent event) {
		AdmLibrary libraryApplication = (AdmLibrary) administratorAccountForm.getApplication();
		if (administratorAccountForm.isValid()) {
			administrator.setForename(administratorAccountForm.getSurnameTextField().getValue().toString());
			administrator.setName(administratorAccountForm.getNameTextField().getValue().toString());
			administrator.setLogin(administratorAccountForm.getLoginTextField().getValue().toString());
			LibraryService.getServiceSubscriber().update(administrator);
			libraryApplication.getMainWindow().showNotification("Vos informations ont étés modifiées avec succès ",
					Notification.TYPE_HUMANIZED_MESSAGE);
		}
		else {
			libraryApplication.getMainWindow().showNotification("Veuillez remplir tous les champs",
					Notification.TYPE_WARNING_MESSAGE);
		}
	}
}
